<?php
/**
 * 远程文件上传
 */
class Qupload{
	public static $host = core_lib_constant::UPLOAD_IP;
	public static $port = core_lib_constant::UPLOAD_PORT;
	public static $path = core_lib_constant::UPLOAD_URL;
	public static $method ='POST';

	/**
	 * 上传图片到服务器
	 * @param array $_file 文件流
	 * @param string $filename 文件名 不含扩展名
	 * @param string $appname
	 * @param string $key 用于归档的key
	 * @param array $size 生成缩略图的大小尺寸,如：array('size[0]'=>'100x100','size[1]'=>'500x500')仅为图片是有效
	 * @param boolean $ismark 是否在缩略图上加水印
	 * @return array
	 */
	public function uploadFileToRemote($_file, $filename='', $appname="myapp", $key='default', $size=array(), $ismark = 1) {
		if ($_file) {
			$query = array();
			$nowtime = time();
			$_file_type = 0;
			$type_img = array('jpg','jpeg','png','bmp');
			if(!$_file['name']){
				return array('state' => 0, 'msg' => '文件不能没有扩展名');
			}
			$ext = strtolower(end(explode(".",$_file['name'])));
			if (in_array($ext,$type_img)) {
				$_file_type = 1;
			} elseif (in_array($ext,array('php','phps','php5','php4'))) {
				return array('state' => 0, 'msg' => '非法扩展名');
			}
			//缩略图尺寸
			if (count($size)>0) {
				foreach ($size as $vkey => $value) {
					$query[$vkey]=$value;
				}
			}
			$query['path'] = "{$appname}/{$key}/".date("Y-m/d/");
			$query['type'] = $_file_type;//为图片时填1,非图片文件填
			$query['name'] = $filename ? $filename : uniqid();
			if (!$ismark) {
				$query['nomask'] = 1;
			}
			$rs = $this->postfile($query, $_file['name'], $_file['tmp_name'], $_file['type']);
			if ($rs['state'] == 1){
				$result['state'] = 1;
				$result['msg'] = $query['path'].$query['name'].'.'.$ext;
				$result['ofilename'] = $_file['name'];
			} else {
				$result['msg'] = $rs['error'];
				$result['state'] = 0;
			}
			return $result;
		}
		return array('state' => 0, 'msg' => '文件为空');
	}
	
	/**
	 * 上传文件接口
	 * @param object $postvar 数据流
	 * @param string $file_name 文件名
	 * @param string $tmp_name 临时文件名
	 * @param string $servers_url 指定上传的地址,该地址需要遵守一定规则
	 * @return array
	 */
	private function postfile($postvar, $file_name = "", $tmp_name, $content_type) {
		$postvar['verify'] = md5(core_lib_constant::UPLOAD_KEY);//合法性检查
		$host = self::$host;
		$method = self::$method;
		$path = self::$path;
		$port = self::$port;
		srand((double)microtime()*1000000);
		$boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
		$header = "POST $path HTTP/1.0\r\n";
		$header .= "Host: $host\r\n";
		$header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";
		foreach($postvar AS $index => $value){
			$data .="--$boundary\r\n";
			$data .= "Content-Disposition: form-data; name=\"".$index."\"\r\n";
			$data .= "\r\n".$value."\r\n";
			$data .="--$boundary\r\n";
		}
		if($tmp_name){
			$data .= "--$boundary\r\n";
			$content_file = join("", file($tmp_name));
			$data .="Content-Disposition: form-data; name=\"userfile\"; filename=\"$file_name\"\r\n";
			$data .= "Content-Type: $content_type\r\n\r\n";
			$data .= "".$content_file."\r\n";
			$data .="--$boundary--\r\n";
		}
		$header .= "Content-length: " . strlen($data) . "\r\n\r\n";
		//连接5次
		$rec = 0;
		do{
			$fp = @fsockopen($host, $port, $errno, $errstr, 10);
			$rec ++;
		}while(!$fp && $rec < 5);
		if (!$fp) {
			return array('state' => 0, 'error' => '连接服务器失败 ');
		}
		fputs($fp, $header.$data);
		while(!feof($fp)){
			$return .= fgets($fp);
		}
		fclose($fp);
		if(strpos($return, '[success]')){
			return array('state' => 1, 'msg' => 'success');
		}else{
			return array('state' => 0, 'error' => $return);
		}
	}
}
?>
